package com.chb.javabasic.jpa;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@Slf4j
@CrossOrigin("*")
@RestController
@RequestMapping("/user2")
@Tag(name = "用户管理")
public class User2Conroller {

    @Autowired
    private User2Service user2Service;

    @GetMapping()
    @Operation(tags = "查询所有用户")
    public List<User2> selectAllUser() {
        return user2Service.getAllUsers();
    }

    @PostMapping()
    @Operation(tags = "提交用户")
    public User2 submitUser(@RequestBody User2 user2) {
        return user2Service.SaveUser2(user2);
    }

    @GetMapping("/search")
    @Operation(tags = "通过用户名查询用户")
    public Optional<User2> selectUserByUsername(@RequestParam Long id) {
        return user2Service.getUserByID(id);
    }

    @DeleteMapping("/{id}")
    @Operation(tags = "删除用户")
    public void deleteUser(@PathVariable Long id) {
        user2Service.deleteById(id);
    }

    @DeleteMapping("/delete/{username}")
    @Operation(tags = "删除用户")
    public void deleteUserByUsername(@PathVariable String username) {
        user2Service.deleteByUsername(username);
    }
}
